package org.dtrd.modules.dict.controller;


import cn.hutool.core.util.NumberUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.dict.entity.vo.DistrictInfo;
import org.dtrd.modules.dict.entity.vo.DistrictInfoVO;
import org.dtrd.modules.dict.service.IDtrdEntRdDicDistrictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_QUERY;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author QR
 * @since 2021-12-06
 */
@Api(tags = "字典数据")
@RestController
@RequestMapping("/dict/district")
public class DistrictInfoController extends BaseController {

    @Autowired
    private IDtrdEntRdDicDistrictService districtInfoService;

    @ApiOperation(value = "查看地区数据", response = DistrictInfo.class)
    @GetMapping("/info")
    @AutoLog(value="查看地区数据", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getDistrict() {
        DistrictInfoVO districtInfo = districtInfoService.getDistrict();
        return Result.OK(districtInfo);
    }

    @ApiOperation(value = "查看省级信息列表", response = DistrictInfo.class)
    @GetMapping("/province")
    @AutoLog(value="查看省级信息列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getProvinceList() {
        List<DistrictInfo> provinces = districtInfoService.listProvinceInfo();
        return Result.OK(provinces);
    }

    @ApiOperation(value = "查看区级信息列表", response = DistrictInfo.class)
    @GetMapping("/city")
    @AutoLog(value="查看区级信息列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getCityList(@RequestParam String provinceId) {
        if (!NumberUtil.isNumber(provinceId)) {
            return Result.OK();
        }
        List<DistrictInfo> cities = districtInfoService.listCityInfo(Integer.parseInt(provinceId));
        return Result.OK(cities);
    }

}
